import { Dept, Login } from "@/api/interface";
import { getAuthMenuListApi, loginApi } from "@/api/modules/login";
import { getLogo } from "@/api/modules/user";
import { initDynamicRouter } from "@/routers/modules/dynamicRouter";
import { GlobalStore } from "@/stores";
import { AuthState } from "@/stores/interface";
import { TabsStore } from "@/stores/modules/tabs";
import { convertMenuList, getAllBreadcrumbList, getSansanLocalMenu, getShowMenuList } from "@/utils/menuUtil";
import { setHome } from "@/utils/routerUtil";
import { getFlatArr } from "@/utils/util";
import { ElMessage } from "element-plus";
import { defineStore } from "pinia";
import { RouterPath } from "../../enums/routerPath";

// AuthStore
export const AuthStore = defineStore({
	id: "AuthState",
	state: (): AuthState => ({
		// 当前页面的 router name，用来做按钮权限筛选
		routeName: "",
		// 按钮权限列表
		authButtonList: {},
		// 菜单权限列表
		authMenuList: [],
		//本地业务系统的静态菜单：父节点菜单路径+子节点数组
		sansanRouterMap: {},
		//分组列表
		deptList: []
	}),
	getters: {
		deptListGet: state => state.deptList,
		// 按钮权限列表
		// authButtonListGet: state => state.authButtonList,
		// 后端返回的菜单列表 ==> 这里没有经过任何处理
		authMenuListGet: state => state.authMenuList,
		// 后端返回的菜单列表 ==> 左侧菜单栏渲染，需要去除 isHide == true
		showMenuListGet: state => getShowMenuList(state.authMenuList),
		// 扁平化之后的一维数组路由，主要用来添加动态路由
		flatMenuListGet: state => getFlatArr(state.authMenuList),
		// 所有面包屑导航列表
		breadcrumbListGet: state => getAllBreadcrumbList(state.authMenuList)
	},
	actions: {
		async setDeptList(value: Dept.ResDeptList[] = []) {
			this.deptList = value;
		},
		// getAuthMenuList
		async getAuthMenuList() {
			const { userInfo } = GlobalStore();
			//初始本地菜单路由
			this.sansanRouterMap = await getSansanLocalMenu();

			let { result } = await getAuthMenuListApi(userInfo.username);
			this.authMenuList = convertMenuList(result.records.children, this.sansanRouterMap);
			// console.log("menu routers.", this.authMenuList);
		},
		// setRouteName
		async setRouteName(name: string) {
			this.routeName = name;
		},
		async login(loginForm: Login.ReqLoginForm) {
			// const router = useRouter();
			const globalStore = GlobalStore();
			const tabsStore = TabsStore();
			// 1.执行登录接口
			const { result, success, message } = await loginApi({ ...loginForm });
			if (!success) {
				return ElMessage.error(message);
			}
			setHome(RouterPath.deviceList);
			globalStore.setToken(result.token);
			globalStore.setUserInfo(result.user);
			globalStore.setExpire(result.expire);
			globalStore.rememberUser(loginForm.rememberUser);
			if (globalStore.setting.rememberUser) {
				globalStore.setting.username = loginForm.username;
			}
			const res = await getLogo();
			if (res.code == 200) {
				globalStore.setLogo(res.result.logo);
				globalStore.setSystemName(res.result.systemName);
			}
			// 2.添加动态路由
			await initDynamicRouter();

			// 3.清除上个账号的 tab 信息
			tabsStore.closeMultipleTab();
			return;
		},
		async logout() {
			const globalStore = GlobalStore();
			globalStore.setToken("");
			globalStore.setUserInfo({ username: "", userId: "" });
		}
	}
});
